package com.peanut.youfupay.controller;


import com.alibaba.fastjson.JSON;
import com.peanut.youfupay.entity.response.ResponseMessage;
import com.peanut.youfupay.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 优付付款异步通知
 * 1、付款只有返回终态（成功、失败）才会进行异步通知，但测试环境只能返回处理中，需要优付手动模拟银行的返回结果
 * 2、通知逻辑：累计5次，商户返回SUCCESS则停止
 * 每隔5秒通知1次，共1次
 * 每隔30秒通知1次，共2次
 * 每隔60秒通知1次，共2次
 *
 * @author peanut
 */
@Controller
@RequestMapping("/pay")
@Slf4j
public class PayNotifyController {
    @Autowired
    BaseService baseService;

    @PostMapping(value = "/notify", produces = "text/xml;charset=UTF-8")
    @ResponseBody
    public String doBusiness(@RequestBody String encryptedMsg) {
        log.info("优付付款异步通知返回报文{}", encryptedMsg);
        ResponseMessage encryptedResponseMessage = JSON.parseObject(encryptedMsg, ResponseMessage.class);
        ResponseMessage responseMessage = baseService.handleResponse(encryptedResponseMessage);
        String notifyData = responseMessage.getResData();
        log.info("优付付款异步通知返回结果-notifyData{}", notifyData);
        //返回给优付的结果
        return "SUCCESS";
    }
    /**优付付款异步通知返回完整报文
     * 商户可用此报文作为请求报文发给异步通知controller测试**/
    /*
    {
            "funCode":"6001",
            "merchantId":"89900000222116027420",
            "resCode":"0000",
            "resData":"Vdwuryj0BzagGQNv31KPKQBiRyDfpi4TkhzKHhinhWBsrr9AN5eCKAu07bSxQDPq0TZOXNm/8RVymunPHD2tRSCJRAIQsG5k/G72xxLo+uNQULCrIprIxEV0/nDdth771aA00KWh881HoC1mYv4YzivxJolNEdz2xoHxLMARe4mO64wS7cYNuB9dhlU98pip9krNzTRpb3Y9IpwZMAsP3jc0KEMk8zER3LtlkZ4H8uimY8gMCsvgiPZKzc00aW926HSgogolPifQzXel7bztdj0OaJBTSvkSKR8Lz9BiPKRSxehU2absxkWzQBM4w7U4CJ+gvQcGk526rYrPlm5RoT77dg8PSyXV3vkXIIYqalk=",
            "resMsg":"成功",
            "sign":"I2XjXVDbeLyrd1PSBLILUgU6S0LJsT1dis0Hdt3N6FQew2M51wJTH8LB9bNlo8EnUPQJ4wfjiKl2iD61GQPD4JSU8/9oh0YO5Rberoc6C9dq0/PeKyKwy4kpkK1x90iF5gjb9pVtg7m4a/ZIzC6au0RBuhZpb/3/Gf8gB7Hv0Mo=",
            "version":"V1.0"
    }
    */
    /**优付付款异步通知返回ResData明文**/
    /*
    {
            "amount":100000,
            "channelOrder":"2020110214333800472592",
            "createDate":"2020-11-02 14:33:38",
            "merId":"89900000222116027420",
            "merNo":"Payment-wzk-1604298817458",
            "merbath":"Batch-Payment-1604298817455",
            "status":"3",
            "sucAmount":100000,
            "successDate":"2020-11-02 14:42:02"
    }
    */

}
